package com.geovis.emergency.spd.controller.pc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillEarlyDisposeService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileBusinessService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandDrillEarlyDisposeVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillEarlyDispose;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPublishInfo;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillEarlyDisposeVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcSchedulingEventVO;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.entity.SystemFileBusiness;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileQueryDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pc/drill/early/dispose")
@RequiredArgsConstructor
@Api(value = "应急指挥模块-前置处置接口", tags = "应急指挥模块-前置处置接口")
@Slf4j
@Validated
public class PcDrillEarlyDisposeController extends BaseController<IPcDrillEarlyDisposeService> {
    private final ISystemFileService systemFileService;

    @ApiOperation(value = "处置列表", notes = "处置列表")
    @GetMapping("/list/{drillId}")
    public Result<List<PcDrillEarlyDisposeVO>> list(@PathVariable("drillId") String drillId) {
        List<PcDrillEarlyDispose> list = baseService.list(
                Wrappers.lambdaQuery(PcDrillEarlyDispose.class)
                        .eq(PcDrillEarlyDispose::getDrillId,drillId)
                        .orderByDesc(PcDrillEarlyDispose::getCreateTime)
        );
        List<PcDrillEarlyDisposeVO> collect = list.stream().map(dispose -> {
            PcDrillEarlyDisposeVO pcDrillEarlyDisposeVO = BeanUtil.toBean(dispose, PcDrillEarlyDisposeVO.class);
            //获取附件信息
            SystemFileQueryDTO systemFileQueryDTO = new SystemFileQueryDTO();
            systemFileQueryDTO.setRelatedId(dispose.getId());
            systemFileQueryDTO.setType(SystemFileBusinessConstant.TYPE_DRILL_EARLY_DISPOSE);
            List<SystemFileDTO> fileDTOList = systemFileService.getList(systemFileQueryDTO);
            pcDrillEarlyDisposeVO.setFileList(fileDTOList.stream().map(s -> BeanUtil.toBean(s, SystemFile.class)).collect(Collectors.toList()));
            return pcDrillEarlyDisposeVO;
        }).collect(Collectors.toList());
//

        return Result.ok(collect);
    }
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping("save")
    public Result save(@RequestBody List<PcDrillEarlyDisposeVO> list) {
        baseService.saveEarlyDisposeBatch(list);
        return Result.ok();
    }
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量删除", notes = "批量删除数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }
}
